/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package comandaXM.utils;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

/**
 *
 * @author xmarti
 */
public class Utils {
    /**
     * Converteix una data en format texte en format data
     * @param date                  Cadena de texte 
     * @return  una data en format java.util.date
     
     */
    
    public static Date getDateSimpleFormat(String date) {
        Date testDate=null;
        try{
            if(date.contains("-")){
                testDate= new SimpleDateFormat("dd-MM-yyyy").parse(date);
            }else{
                testDate= new SimpleDateFormat("dd/MM/yyyy").parse(date);
            }    
        }catch (ParseException e){
            return null;
        }        
        return testDate;
    }
    
     public static String getStringDate(Date date){
        String sDate="";
       
        if(date != null)
            sDate= new SimpleDateFormat("dd/MM/yyyy").format(date);                                
        
        return sDate;        
    }
     public static Color getColorNou(){
         
          int red = 255;
          int green = 255;
          int blue = 166;
          float[] hsb = Color.RGBtoHSB(red, green, blue, null);             
          return Color.getHSBColor(hsb[0],hsb[1],hsb[2]);
     }
     public static Color getColorEdicio(){
         
          int red = 255;
          int green = 204;
          int blue = 153;
          float[] hsb = Color.RGBtoHSB(red, green, blue, null);             
          return Color.getHSBColor(hsb[0],hsb[1],hsb[2]);
                  
     }
     public static Color getColorCerca(){
         int red = 236;
          int green = 233;
          int blue = 216;
          float[] hsb = Color.RGBtoHSB(red, green, blue, null);             
          return Color.getHSBColor(hsb[0],hsb[1],hsb[2]);
     }
     public static Color getColorControlLectura(){
         int red = 255;
          int green = 255;
          int blue = 255;
          float[] hsb = Color.RGBtoHSB(red, green, blue, null);             
          return Color.getHSBColor(hsb[0],hsb[1],hsb[2]);
     }
     public static Color getColorControlEdicio(){
         int red = 255;
          int green = 255;
          int blue = 204;
          float[] hsb = Color.RGBtoHSB(red, green, blue, null);             
          return Color.getHSBColor(hsb[0],hsb[1],hsb[2]);
     }
     
     public static Color getColorError(){
         return Color.RED;
     }
     public static Color getColorOk(){         
         //return Color.getHSBColor(0,102,255);
         return Color.BLUE;
     }
     
     public static int confirmSaveOptionPane(String description){
        Object[] options = {TDSLanguageUtils.getMessage("jOptionPane.JLabel.yes.caption"),TDSLanguageUtils.getMessage("jOptionPane.JLabel.no.caption")};
        return JOptionPane.showOptionDialog(null,description,TDSLanguageUtils.getMessage("jOptionPane.JLabel.save.caption"),
                JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,
                options,  //the titles of buttons
                options[0]); //default button title 
     }
     public static int confirmRemoveOptionPane(String description){
        Object[] options = {TDSLanguageUtils.getMessage("jOptionPane.JLabel.yes.caption"),TDSLanguageUtils.getMessage("jOptionPane.JLabel.no.caption")};
        return JOptionPane.showOptionDialog(null,description,TDSLanguageUtils.getMessage("jOptionPane.JLabel.remove.caption"),
                JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,
                options,  //the titles of buttons
                options[0]); //default button title 
     }
     public static boolean isInteger(String valor){
         try{
             Integer.parseInt(valor);
             return true; 
         }catch(NumberFormatException ex ){
             return false;  
         }         
     }
     public static int getInteger (String valor){
         try{             
             return Integer.parseInt(valor);
         }catch(NumberFormatException ex ){
             return 0; 
         }         
     }
     public static Border setBorderError(){
         return javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, java.awt.Color.red); 
     }
     public static Border setBorderOk(){            
        return javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1,java.awt.Color.black); 
     }
     public static boolean isDouble(String valor){
          try{
             Double.parseDouble(valor);
             return true; 
         }catch(NumberFormatException ex ){
             return false;  
         }  
     }
     public static double getDouble(String valor){
          try{             
             return  Double.parseDouble(valor);
         }catch(NumberFormatException ex ){
             return 0;  
         }  
     }
     
     public static Set forwardKeys(){
         Set <KeyStroke> keys= new HashSet<KeyStroke>();
         keys.add(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
         keys.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
         keys.add(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0));
         return keys; 
     }
     public static Set forwardKeyTab(){
         Set <KeyStroke> keys= new HashSet<KeyStroke>();         
         keys.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));         
         return keys; 
     }
     public static String pathInformes(){
         return System.getProperty ("user.dir")+"\\build\\classes\\comandaxm\\vista\\informes"; 
     }
     public static Set backwardKeys(){
         Set <KeyStroke> keys= new HashSet<KeyStroke>();         
         keys.add(KeyStroke.getKeyStroke(KeyEvent.VK_SHIFT | KeyEvent.VK_TAB , 0));
         keys.add(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0));
         return keys; 
     }
     public static Set backwardKeyTab(){
         Set <KeyStroke> keys= new HashSet<KeyStroke>();         
         keys.add(KeyStroke.getKeyStroke(KeyEvent.VK_SHIFT | KeyEvent.VK_TAB , 0));         
         return keys; 
     }
    private static class BorderImpl implements Border {

        public BorderImpl() {
        }

        @Override
        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

                @Override
                public Insets getBorderInsets(Component c) {
                    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                }

                @Override
                public boolean isBorderOpaque() {
                    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                }
    }
     
     
}
